import request from '@/utils/request'
import { mockAPI } from '@/mock/userMockAPI'

export enum UserStatus {
  ALL = 'ALL',              //全部用户，每个用户只会出现一次，但其他的可能会有多用户-会议数据
  PENDING = 'PENDING',       // 未允许参会
  APPROVED = 'APPROVED',     // 已允许参会
  UNSIGNED = 'UNSIGNED',     // 未签到
  SIGNED = 'SIGNED'         // 已签到
}

export interface UserMeetingRecord {
  id: string
  nickname: string        
  phone: string         
  reason: string        // 申请理由
  status: UserStatus    
  meetId: string        // 会议ID
  absenceReason: string // 缺席原因
  applyTime: string     // 申请时间 TODO: 显示在前端
  updateTime: string    // 状态更新时间 TODO: 显示在前端
}

export interface ListUserRequest {
  page: number
  size: number
  meetId?: string     // 会议ID，可用于筛选特定会议的申请记录
  status?: UserStatus // 用户状态
}

export interface ListUserResponse {
  total_record: number
  total_page: number
  current_data: UserMeetingRecord[]  
}

// 添加一个开关来控制是否使用mock数据
// const USE_MOCK = false

// 获取用户列表
export const getUserList = (params: ListUserRequest) => {

  return request.post<any, ListUserResponse>('/api/user/users', params)
}

// 允许参会
export const approveUser = (params: { phone: string; meetId: string }) => {

  return request.post('/api/user/approve', params)
}

// 批量允许参会
export const batchApproveUsers = (params: { records: Array<{ phone: string; meetId: string }> }) => {

  return request.post('/api/user/batch_approve', params.records)
}

// 拒绝申请
export const rejectUser = (params: { phone: string; meetId: string }) => {

  return request.post('/api/user/reject', params)
}

// 批量拒绝申请
export const batchRejectUsers = (params: { records: Array<{ phone: string; meetId: string }> }) => {

  return request.post('/api/user/batch_reject', params.records)
}

// 签到
export const signInUser = (params: { phone: string; meetId: string }) => {

  return request.post('/api/user/sign_in', params)
}

// 二维码签到响应接口
export interface QRCodeSignResponse {
  code: number
  data: any
  msg: string
}

// 二维码签到
export const signInWithQRCode = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post<any, QRCodeSignResponse>('/api/meet/sign', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出用户参会数据，id->用户信息->excel表
 * @param ids 选中的用户ID列表
 * @returns Excel文件流
 */
export const exportUserData = (ids: string[]) => {

  return request.post('/api/user/export', ids, {
    responseType: 'blob',
  })
}